import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

// const host = 'http://localhost:7003';
const host = 'https://enroll.zekdot.com:7007';
Vue.prototype.host = host; 
Vue.prototype.apiurl = host + '/api'; 
Vue.prototype.userurl = Vue.prototype.apiurl + '/user-consumer'
Vue.prototype.enrollurl = Vue.prototype.apiurl + '/register-consumer'
Vue.prototype.fileurl = Vue.prototype.apiurl + '/file-consumer'
// Vue.prototype.fileurl = 'https://enroll.zekdot.com:7024/'
Vue.prototype.pageurl = 'http://localhost:8080/#/pages'
function request(url,data,method,successFun,noLogin){
  if(!noLogin){
      var tokenJson;
			// 载入题目
			uni.getStorage({
				key : 'userToken',
				success :function(res){
          tokenJson = res.data;
          // console.log(data)
          data['token'] = tokenJson['token'];
          data['userId'] = tokenJson['userId'];
          uni.request({
            url :url,
            header : {
              'content-type' : 'application/x-www-form-urlencoded'
            },
            data : data,
            method : method,
            dataType : "json",
            success : successFun
          })
				},
				fail : function(){

				}
      });
      
  }else{
    uni.request({
      url :url,
      header : {
        'content-type' : 'application/x-www-form-urlencoded'
      },
      data : data,
      method : method,
      dataType : "json",
      success : successFun
    })
  }
  
}

async function asyncRequest(url,data,method,successFun,noLogin){
  return new Promise(() =>{
    if(!noLogin){
      var tokenJson;
      // 载入题目
      uni.getStorage({
        key : 'userToken',
        success :function(res){
          tokenJson = res.data;
          data['token'] = tokenJson['token'];
          data['userId'] = tokenJson['userId'];
          uni.request({
            url :url,
            header : {
              'content-type' : 'application/x-www-form-urlencoded'
            },
            data : data,
            method : method,
            dataType : "json",
            success : successFun
          })
        },
        fail : function(){

        }
      });
      
    }else{
      uni.request({
        url :url,
        header : {
          'content-type' : 'application/x-www-form-urlencoded'
        },
        data : data,
        method : method,
        dataType : "json",
        success : successFun
      })
    }
    
  })
}
Vue.prototype.req = request;
Vue.prototype.$get = function(url,data,successFun,noLogin){
  request(url,data,"GET",successFun,noLogin);
}

Vue.prototype.$post = function(url,data,successFun,noLogin){
  request(url,data,"POST",successFun,noLogin);
}

Vue.prototype.$get(Vue.prototype.host + '/config/server.json',{},function(res){
  // console.log(res.data)
  Vue.prototype.serverConfig = res.data;
  // console.log(vm.serverConfig)
},true)

Vue.prototype.$asyncRequest = asyncRequest;
App.mpType = 'app'

const app = new Vue({
  ...App
})
app.$mount()
